<?php

namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request; //命名空间的三元素：常量，方法和类
use DB;
use App\Http\Model\Book;

class ShopymController extends Controller
{
    // 显示商品信息
    public function LookShopym(){
        $result = Book::paginate(6);
        return view('admin/shopym/shopym',compact('result'));
    }
 
    // 显示商品修改内容
    public function Xgshopym($id){
        $result =  Book::leftJoin('tb_type','tb_book.typeid','=','tb_type.typeid')->where('bookid',$id)->first();
        return view('admin/shopym/xgshopym',compact('result'));
    }

    // 修改商品
    public function ShopymUp(Request $request){
        $data = $request->all();
        $ids = $data['bookid']; 
         // 上传图片，保存图片
         $uploadFile = $data['photo'];
         $data['photo'] = $uploadFile -> store('img','upload');
         // 删除token等多余的列
         unset($data['_token']);
        //  unset($data['ok']);
         $result =  Book::where('bookid',$ids)->update($data);
         if($result>0){
             return redirect('admin/shopym');
         }
         else
         {
             return back();
         }
    }
    // 删除商品内容
      public function Delshopym($bookid){
        Book::find($bookid)->delete();
        return ['status'=>0,'msg'=>'删除成功'];
        // $result =  Book::where('bookid',$id)->delete();
        // if($result > 0){
        //     return back();
        // }
    }

     // 复选框删除
     public function Delallym(){
        foreach ($_POST['checked'] as $key => $id) {
          $result =  Book::where('bookid',$id)->delete();
        }
        if($result>0){
            return redirect('admin/shopym');
        }
        else
        {
            return back();
        }
       
    }


     // 新增
     public function Addym(Request $request){
        $data = $request->all();
        // dd($data);
        // 上传图片，保存图片
        $uploadFile = $data['photo'];
        $data['photo'] = $uploadFile -> store('img','upload');
        // 删除token等多余的列
        unset($data['_token']);
        unset($data['ok']);
        $result =  Book::insert($data);
        if($result>0){
            return redirect('admin/shopym');
        }
        else
        {
            return back();
        }
        
    }

   
}